<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.xpy.utils.ConnSqlServer" %>

<%
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");

    String id = request.getParameter("id");
    String name = "";
    String pwd = "";
    int power = 0;

    if (id != null) {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            conn = ConnSqlServer.getConnection();
            String query = "SELECT name, pwd, power FROM admin WHERE id = ?";
            pstm = conn.prepareStatement(query);
            pstm.setInt(1, Integer.parseInt(id));
            rs = pstm.executeQuery();
            if (rs.next()) {
                name = rs.getString("name");
                pwd = rs.getString("pwd");
                power = rs.getInt("power");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) try { rs.close(); } catch (Exception e) { e.printStackTrace(); }
            if (pstm != null) try { pstm.close(); } catch (Exception e) { e.printStackTrace(); }
            if (conn != null) try { conn.close(); } catch (Exception e) { e.printStackTrace(); }
        }
    }
%>

<html>
<head>
    <title>修改管理员</title>
    <link rel="stylesheet" href="css/style2.css">
    <style>
        .form-container {
            width: 30%;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 10px;
            box-shadow: 2px 2px 12px #aaa;
        }
        .form-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: inline-block;
            width: 40%;
            font-weight: bold;
            text-align: right;
            margin-right: 10px;
        }
        .form-group input, .form-group select {
            width: 50%;
            padding: 5px;
            box-sizing: border-box;
        }
        .form-buttons {
            text-align: center;
            margin-top: 20px;
        }
        .form-buttons button {
            margin: 0 10px;
            padding: 5px 15px;
        }
    </style>
</head>
<body>
<div class="form-container">
    <h2>修改管理员</h2>
    <form action="admin_modify.jsp" method="post">
        <div class="form-group">
            <label for="id">管理员ID</label>
            <input type="text" id="id" name="id" value="<%= id %>" readonly>
        </div>
        <div class="form-group">
            <label for="name">管理员姓名</label>
            <input type="text" id="name" name="name" value="<%= name %>" required>
        </div>
        <div class="form-group">
            <label for="pwd">密码</label>
            <input type="password" id="pwd" name="pwd" value="<%= pwd %>" required>
        </div>
        <div class="form-group">
            <label for="power">权限</label>
            <select id="power" name="power">
                <option value="1"<%= (power == 1) ? " selected" : "" %>>超级管理员</option>
                <option value="0"<%= (power == 0) ? " selected" : "" %>>普通管理员</option>
            </select>
        </div>
        <div class="form-buttons">
            <button type="submit">确定</button>
            <button type="reset">重置</button>
            <button type="button" onclick="window.location.href='admin_admin.jsp'">返回</button>
        </div>
    </form>
</div>

<%
    if ("POST".equalsIgnoreCase(request.getMethod())) {
        String modifyId = request.getParameter("id");
        String modifyName = request.getParameter("name");
        String modifyPwd = request.getParameter("pwd");
        int modifyPower = Integer.parseInt(request.getParameter("power"));

        if (modifyId != null && !modifyId.isEmpty() && modifyName != null && !modifyName.isEmpty() && modifyPwd != null) {
            Connection conn = null;
            PreparedStatement pstm = null;
            try {
                conn = ConnSqlServer.getConnection();
                // 使用用户输入的数据准备更新数据库的SQL语句
                String sql = "UPDATE admin SET name=?, pwd=?, power=? WHERE id=?";
                pstm = conn.prepareStatement(sql);
                pstm.setString(1, modifyName);
                pstm.setString(2, modifyPwd);
                pstm.setInt(3, modifyPower);
                pstm.setInt(4, Integer.parseInt(modifyId));

                // 执行更新操作
                int result = pstm.executeUpdate();
                if (result > 0) {
                    // 更新成功
                    out.println("<script>alert('管理员信息修改成功！'); window.location.href='admin_admin.jsp';</script>");
                } else {
                    // 没有行被更新，可能是ID不匹配
                    out.println("<script>alert('没有找到要修改的记录，请确保ID正确。'); window.history.back();</script>");
                }
            } catch (Exception e) {
                // 打印异常信息到服务器日志
                e.printStackTrace();
                out.println("<script>alert('发生错误：' + " + e.getMessage() + ");</script>");
            } finally {
                // 关闭PreparedStatement和Connection
                if (pstm != null) try { pstm.close(); } catch (Exception e) { e.printStackTrace(); }
                if (conn != null) try { conn.close(); } catch (Exception e) { e.printStackTrace(); }
            }
        } else {
            // 输入不完整，返回错误信息
            out.println("<script>alert('请填写所有字段。'); window.history.back();</script>");
        }
    }
%>

</body>
</html>